package domain;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import util.*;

import domain.user.User;

import Persistent.*;

public class Forum {

	XmlForum forum;
	
	//Constructor
	public Forum() 
	{
		ObjectFactory factory = new ObjectFactory();
		this.forum = factory.createXmlForum();
	}
	
	public Forum(String name)
	{
		ObjectFactory factory = new ObjectFactory();
		this.forum = factory.createXmlForum();
		forum.setForumName(name);
	}
	
	//Getters
	public String getName()
	{
		return this.forum.getForumName();
	}
	
	public List<XmlPost> getPosts() 
	{
		return this.forum.getPosts();
	}
	
	public List<XmlLoggedOnUser> getUsers()
	{
		return this.forum.getUsers();
	}
		
	//Setters
	public void setName(String name)
	{
		this.forum.setForumName(name);
	}
	
	
	/**Function which add new thread
	 * 
	 * @param post
	 * @return true (as specified by Collection.add(E))
	 */
	public boolean addPost(XmlPost post)
	{
		return this.forum.getPosts().add(post);
	}
	
	/**Function which add new user
	 * 
	 * @param user
	 * @return true (as specified by Collection.add(E))
	 */
	public boolean addUser(XmlLoggedOnUser user)
	{
		return this.forum.getUsers().add(user);
	}
	
	public boolean Register(String username,String pass,String fname,String lname, String email)
	{
		XmlLoggedOnUser user = new XmlLoggedOnUser();
		user.setUserName(username);
		user.setPassword(encryptPass(pass));
		user.setFName(fname);
		user.setLName(lname);
		user.setEMail(email);
		
		this.getUsers().add(user);
		return true;
	}
	
	public boolean Login(String username, String password) throws NoSuchAlgorithmException, NotFoundException, BadPasswordException
	{
		XmlLoggedOnUser currentUser = getUserByUsername(username);
		if(currentUser == null)
		{
			throw new NotFoundException(username);
		}
		if (!checkPassword(password,currentUser.getPassword()))
		{
			throw new BadPasswordException(password);
		}
		
		return true;
	}
	
	private String encryptPass(String password)
	{
		  MessageDigest md;
		  String ans = "";
	        try {
	            md = MessageDigest.getInstance("SHA");
	           
	             md.update(password.getBytes());
	             byte[] b=md.digest();
	             for (int i = 0; i < b.length; i++) 
	             {
	            	 ans += (char)b[i];
	             }	
	        } catch (NoSuchAlgorithmException e) {
	            
	            e.printStackTrace();
	        }
	        return ans;
	}
	
	private boolean checkPassword(String fromUser, String fromDB )
    {
         MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA");
           
             md.update(fromUser.getBytes());
             byte[] b=md.digest();
             return b.equals(fromDB.getBytes());
        } catch (NoSuchAlgorithmException e) {
           
            e.printStackTrace();
        }
       
        return false;
    }
	
	private XmlLoggedOnUser getUserByUsername(String userName)
	{
		Iterator<XmlLoggedOnUser> it =  this.getUsers().iterator();
		XmlLoggedOnUser user;
		while (it.hasNext())
		{
			user = it.next();
			if (user.getUserName().equals(userName))
				return user;
		}
		return null;
	}

}
